import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index/Index.vue'
import login from '../views/Login/login.vue'
import ElementUI from 'element-ui'//全局element-ui
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(VueRouter)
Vue.use(ElementUI)


// 本项目中路由的配置；
// 在路由配置的时候，给需要验证的路由添加一个自定义的meta对象，在meta对象中可以设置一些状态，来进行一些操作。我们用它来做权限校验；
const routes = [
  { path: '/', name: 'login', component: login, meta: { sortNo: 0 }, hidden: true },
  { path: '/401', name: '401', component: () => import('../views/ErrorPage/401.vue'), meta: { sortNo: 0 }, hidden: true }, //401
  { path: '/404', name: '404', component: () => import('../views/ErrorPage/404.vue'), meta: { sortNo: 0 }, hidden: true }, //404
  {
    path: '/Index', name: 'Index', component: Index, redirect: '/Index/Rotation',
    children: [
      {
        path: '/Approve/CarrierApproval',
        name: 'CarrierApproval',
        component: () => import('../views/Approve/CarrierApproval.vue')
      }, //承运审批
      {
        path: '/Approve/OwnerApproval',
        name: 'OwnerApproval',
        component: () => import('../views/Approve/OwnerApproval.vue')
      }, //货主审批
      {
        path: '/contract/ContractofCarriage',
        name: 'ContractofCarriage',
        component: () => import('../views/contract/ContractofCarriage.vue')
      }, //承运
      {
        path: '/contract/OwnerContract',
        name: 'OwnerContract',
        component: () => import('../views/contract/OwnerContract.vue')
      }, //货主
      {
        path: '/Settlement/Payment',
        name: 'Payment',
        component: () => import('../views/Settlement/Payment.vue')
      }, //主页
      {
        path: '/Index/Rotation',
        name: 'Rotation',
        component: () => import('../views/Index/Rotation.vue')
      }, //主页
      {
        path: '/BasicInformation/Vehicle',
        name: 'Vehicle',
        component: () => import('../views/BasicInformation/Vehicle.vue'),
        meta: { requiresAuth: true }
      }, //车辆管理
      { path: '/BasicInformation/Owner', name: 'Owner', component: () => import('../views/BasicInformation/Owner.vue'), meta: { requiresAuth: true } }, //货主管理
      { path: '/BasicInformation/Outsource', name: 'Outsource', component: () => import('../views/BasicInformation/Outsource.vue'), meta: { requiresAuth: true } }, //外协单位管理
      { path: '/BasicInformation/OilCost', name: 'OilCost', component: () => import('../views/BasicInformation/OilCost.vue'), meta: { requiresAuth: true } }, //油费管理
      { path: '/BasicInformation/Circuit', name: 'Circuit', component: () => import('../views/BasicInformation/Circuit.vue'), meta: { requiresAuth: true } }, //线路管理

      { path: '/Personnel/Kanban', name: 'Kanban', component: () => import('../views/Personnel/Kanban.vue'), meta: { requiresAuth: true } }, //人事看板
      { path: '/Personnel/Register', name: 'Circuit', component: () => import('../views/Personnel/Register.vue'), meta: { requiresAuth: true } }, //员工登记
      { path: '/Personnel/Induction', name: 'Circuit', component: () => import('../views/Personnel/Induction.vue'), meta: { requiresAuth: true } }, //入职办理
      { path: '/Personnel/Quit', name: 'Circuit', component: () => import('../views/Personnel/Quit.vue'), meta: { requiresAuth: true } }, //离职办理
      { path: '/Personnel/Worker', name: 'Circuit', component: () => import('../views/Personnel/Worker.vue'), meta: { requiresAuth: true } }, //转正办理

      { path: '/Maintain/Repair', name: 'Kanban', component: () => import('../views/Maintain/Repair.vue'), meta: { requiresAuth: true } }, //维修记录
      { path: '/Maintain/Maintain', name: 'Circuit', component: () => import('../views/Maintain/Maintain.vue'), meta: { requiresAuth: true } }, //保养记录
      { path: '/Maintain/Violation', name: 'Circuit', component: () => import('../views/Maintain/Violation.vue'), meta: { requiresAuth: true } }, //违章记录
      { path: '/Maintain/Accident', name: 'Circuit', component: () => import('../views/Maintain/Accident.vue'), meta: { requiresAuth: true } }, //事故记录
      { path: '/Maintain/Tyre', name: 'Circuit', component: () => import('../views/Maintain/Tyre.vue'), meta: { requiresAuth: true } }, //轮胎使用记录
      { path: '/Maintain/Cost', name: 'Circuit', component: () => import('../views/Maintain/Cost.vue'), meta: { requiresAuth: true } }, //费用记录

      { path: '/Set/Role', name: 'Role', component: () => import('../views/Set/Role.vue'), meta: { requiresAuth: true } }, //角色管理
      { path: '/Set/Department', name: 'Department', component: () => import('../views/Set/Department.vue'), meta: { requiresAuth: true } }, //部门管理
      { path: '/Set/Position', name: 'Position', component: () => import('../views/Set/Position.vue'), meta: { requiresAuth: true } }, //职位管理
      { path: '/Set/UserInfo', name: 'UserInfo', component: () => import('../views/Set/UserInfo.vue'), meta: { requiresAuth: true } }, //用户管理
    ]
    
  },
  
]



const router = new VueRouter({
  //mode: "history", //后端支持可开
  scrollBehavior: () => ({ y: 0 }),
  routes
})


// // 每次刷新的时候重新保存菜单
// let menuList = [];
// if (window.name == "") {
//   // 在首次进入页面时我们给window.name设置一个固定值(isRefresh)
//   window.name = "isRefresh";
//   //menuList = this.store.state.menuList;
//   console.log(menuList);
// } else if (window.name == "isRefresh") {
//   this.store.commit("saveMenuList", sessionStorage.getItem("menu"));
//   menuList = JSON.parse(sessionStorage.getItem("menu"));
//   console.log(menuList);
// }

// // 使用路由守卫，to表示即将进入的路由对象，from即将离开的路由
// // next是进行管道中的一个钩子，如果执行完了，则导航的状态就是确认的；否则为false，终止导航。
// router.beforeEach((to, from, next) => {
//   // 首先需要判断当用户直接访问该页面时，当前用户是否有该项权限，根据在路由中添加的meta.requiresAuth属性，访问的页面有此属性，会进行下面if判断；
//   if (to.matched.some((res) => res.meta.requiresAuth)) {
//     if (menuList && menuList.length != 0) {
//       // 下面我要在这里判断，用户访问的to.path，跟我菜单中的path是否一致，
//       // 若一致，那么该登录者可以访问此页面，
//       // 若不一致，将跳出登录页，或提示用户无权限访问该页面
//       let isRequire = deepQuery(menuList, to.path);
//       if (isRequire) {
//         // 若存在，继续访问
//         next();
//       } else {
//         // 提示无权限访问
//         ElementUI.Message.error({
//           message: "无权限访问",
//           type: "warning",
//         });
//         // 跳出登录页
//         next("/401");
//       }
//     } else {
//       next();
//     }
//   } else {
//     // 若没requiresAuth属性，不走上面的判断，说明是可以访问的页面
//     next();
//   }
// });

// // 查找菜单数组中path是否存在,使用递归循环查询该数组是否有children，判断path跟我菜单中的path是否一致；
// function deepQuery(tree, path) {
//   var isGet = false;
//   var retNode = null;
//   function deepSearch(tree, path) {
//     for (var i = 0; i < tree.length; i++) {
//       if (tree[i].children && tree[i].children.length > 0) {
//         deepSearch(tree[i].children, path);
//       }
//       if (path === tree[i].path || isGet) {
//         isGet || (retNode = tree[i]);
//         isGet = true;
//         break;
//       }
//     }
//   }
//   deepSearch(tree, path);
//   return retNode;
// }

// //拦截路由守卫
// router.beforeEach((to, from, next) => {
//   //to表示将要访问的路径
//   //from代表从哪个路径跳转而来
//   //next表示一个函数 放行
//   if (to.path === '/') return next();    //如果访问的是登陆页面就放行
//   const tokenStr = window.sessionStorage.getItem('token');//获取token
//   if (!tokenStr) {
//     //登录失败
//     ElementUI.Message.error('请先登录')
//     return next('/');
//   } //如果token不存在就证明没有登陆过，强制跳转到登陆页面
//   next();                               //存在则放行
// })



export default router




